import 'package:flutter/material.dart';
import 'package:sample2/components/alert_helper.dart';
import 'package:sample2/services/chat_service.dart';
import 'package:sample2/utils/string_util.dart';

class AddChatGroup extends StatefulWidget {
  final String channelId;
  final String adminId;
  VoidCallback? onSuccessCreateGroup;

  AddChatGroup(
      {super.key,
      required this.adminId,
      required this.channelId,
      this.onSuccessCreateGroup});
  @override
  State<StatefulWidget> createState() {
    return AddChatGroupState();
  }
}

class AddChatGroupState extends State<AddChatGroup> {
  String? _groupName;
  String? _groupDesc;

  void _createGroup() {
    if (StringUtil.isNullOrEmpty(_groupName)) {
      AlertHelper.showAlert(
          context: context, message: "Please input the group name");
      return;
    }
    String channelId = widget.channelId;
    String adminId = widget.adminId;
    
    ChatService.createGroup(
            adminId: adminId,
            channelId: channelId,
            groupName: _groupName!,
            groupDesc: _groupDesc)
        .then((value) {
      AlertHelper.showAlert(
        context: context,
        message: "Create new group success",
        onPressed: () {
          Navigator.pop(context);
          widget.onSuccessCreateGroup?.call();
        },
      );
    }).catchError((error) {
      debugPrint("got error $error while create new chat group");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("add a group"),
      ),
      body: ListView(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.all(13),
        children: [
          Row(
            children: [
              Container(
                constraints: const BoxConstraints(maxWidth: 100),
                child: const Text("Group Name:"),
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: TextField(
                  onChanged: (value) {
                    _groupName = value;
                  },
                  decoration: const InputDecoration(
                      prefixStyle: TextStyle(color: Colors.black),
                      border: InputBorder.none,
                      hintText: "Please input the group name"),
                ),
              )
            ],
          ),
          Row(
            children: [
              Container(
                constraints: const BoxConstraints(maxWidth: 150),
                child: const Text(
                  "Group Description:",
                ),
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: TextField(
                  onChanged: (value) {
                    _groupDesc = value;
                  },
                  decoration: const InputDecoration(
                      prefixStyle: TextStyle(color: Colors.black),
                      border: InputBorder.none,
                      hintText: "Please input the group description"),
                ),
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TextButton(onPressed: _createGroup, child: const Text("Confirm"))
            ],
          ),
          const Spacer(
            flex: 1,
          )
        ],
      ),
    );
  }
}
